/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.lastproject;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author WIN7
 */
public class TabInfo {
    public DefaultTableModel ModelTable;
    public JTable jTable;
    public int Type = 0;
    public String NameTable;
    private ServerForm parent;
    
    public TabInfo(ServerForm _parent, String nameTable)
    {
        NameTable = nameTable;
        parent = _parent;
        ModelTable = new DefaultTableModel();
        jTable = new JTable(ModelTable);
       
    }
    public void SetCreateStrucTable()
    {
        Type  = 1;
         MouseAdapter  _maTable = new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                int iRow = jTable.rowAtPoint(e.getPoint());
                int iColumn = jTable.rowAtPoint(e.getPoint());
                
                String sNameColumn = (String)jTable.getValueAt(iRow, 0);
                String sDataType =  (String)jTable.getValueAt(iRow, 1);
                String sPrimary = "null";
                Object oPrimary = jTable.getValueAt(iRow, 2);
                if(oPrimary != null)
                {
                    sPrimary = oPrimary.toString();
                }
                
                int iAmount = jTable.getRowCount();
                if(iAmount == iRow + 1)
                {
                    boolean bNewRow = isFillFullData(sNameColumn, sDataType, sPrimary);
                    if(bNewRow)
                    {
                        String[] newRowData = {null, null, null};
                        ModelTable.addRow(newRowData);
                    }
                }
            }
        };
        jTable.addMouseListener(_maTable);
        
        jTable.setBackground(new Color(204, 204, 255));
        jTable.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));

        jTable.setFont(new Font("Tahoma", 1, 14));
        
        String[] sHeader_CreateTable = {"Column name", "Data Type", "Primary key"};
        String[][] sRow_CreateTable = {{null, null, null, null}};
        ModelTable = new DefaultTableModel(sRow_CreateTable, sHeader_CreateTable );
        jTable.setModel(ModelTable);
        JCheckBox _jchbPrimary = new JCheckBox();
        DefaultCellEditor dePrimary = new DefaultCellEditor(_jchbPrimary);
        JComboBox _jcbDataType = new JComboBox();
        
        _jcbDataType.addItem("string");
        _jcbDataType.addItem("integer");
        DefaultCellEditor deDataType = new DefaultCellEditor(_jcbDataType);
        
        jTable.getColumnModel().getColumn(2).setCellEditor(dePrimary);
        jTable.getColumnModel().getColumn(1).setCellEditor(deDataType);  
         TableData_StructClickLisstener clickLisstener  = new TableData_StructClickLisstener(jTable, parent,NameTable);   
         clickLisstener.setCreateTable();
         jTable.addMouseListener(clickLisstener);
    }
    public void SetUpdateStrucTable()
    {
        Type  = 2;
         MouseAdapter  _maTable = new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                int iRow = jTable.rowAtPoint(e.getPoint());
                int iColumn = jTable.rowAtPoint(e.getPoint());
                
                String sNameColumn = (String)jTable.getValueAt(iRow, 0);
                String sDataType =  (String)jTable.getValueAt(iRow, 1);
                String sPrimary = "null";
                Object oPrimary = jTable.getValueAt(iRow, 2);
                if(oPrimary != null)
                {
                    sPrimary = oPrimary.toString();
                }
                
                int iAmount = jTable.getRowCount();
                if(iAmount == iRow + 1)
                {
                    boolean bNewRow = isFillFullData(sNameColumn, sDataType, sPrimary);
                    if(bNewRow)
                    {
                        String[] newRowData = {null, null, null};
                        ModelTable.addRow(newRowData);
                    }
                }
            }
        };
        jTable.addMouseListener(_maTable);
        
        jTable.setBackground(new Color(204, 204, 255));
        jTable.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));

        jTable.setFont(new Font("Tahoma", 1, 14));
        
        String[] sHeader_CreateTable = {"Column name", "Data Type", "Primary key"};
        String[][] sRow_CreateTable = {{null, null, null, null}};
        ModelTable = new DefaultTableModel(sRow_CreateTable, sHeader_CreateTable );
        jTable.setModel(ModelTable);
        JCheckBox _jchbPrimary = new JCheckBox();
        DefaultCellEditor dePrimary = new DefaultCellEditor(_jchbPrimary);
        JComboBox _jcbDataType = new JComboBox();
        
        _jcbDataType.addItem("string");
        _jcbDataType.addItem("integer");
        DefaultCellEditor deDataType = new DefaultCellEditor(_jcbDataType);
        
        jTable.getColumnModel().getColumn(2).setCellEditor(dePrimary);
        jTable.getColumnModel().getColumn(1).setCellEditor(deDataType); 
        
        
        
         TableData_StructClickLisstener clickLisstener  = new TableData_StructClickLisstener(jTable, parent,NameTable);   
//         clickLisstener.setUpdateTable();
         jTable.addMouseListener(clickLisstener);
    }
   
    private boolean isFillFullData(String nameColumn, String dataType, String bPrimary )
    {
        if( nameColumn!= null && !nameColumn.equals("") && dataType != null && !dataType.equals("") && bPrimary != null && !bPrimary.equals(""))
        {
            return true;
        }        
        return false;
    }
     public void SetTableEditData(final XMLManage _XMLManage, final String _sNameTableCurrent)
    {
       Type  = 3;
       RowDataStructListener listener = new RowDataStructListener(jTable, parent, _sNameTableCurrent);
       jTable.addMouseListener(listener);
       MouseAdapter _maDataRow = new MouseAdapter()        
        {
             public void mouseClicked(MouseEvent e)
             {
                int iRow = jTable.rowAtPoint(e.getPoint());
                int iColumn = jTable.rowAtPoint(e.getPoint());
                
                
                boolean bNewRow = true;
                Vector<String> vtDataInput = new Vector<String>(0);
                int iAmountColumn = jTable.getColumnCount();
                for(int i = 0; i < iAmountColumn; i++)
                {
                    Object ob = jTable.getValueAt(iRow, i);
                    String value = null;
                    if(ob != null)
                        value = ob.toString();
                    
                    if(value == null)
                    {
                        bNewRow = false;
                        break;
                    }
                    
                    vtDataInput.add(value);                    
                }
                
                int iAmount = jTable.getRowCount();
                if(iAmount == iRow + 1)
                {              
                    //nếu điền đủ dữ liệu dòng trên --> sẽ tự động thêm 1 dòng mới
                    if(bNewRow)
                    {
                        String[] newRowData = new String[iAmountColumn  + 1];
                        newRowData[iAmountColumn] =  String.valueOf(_XMLManage.createID(_sNameTableCurrent));                        
                        
                        ModelTable.addRow(newRowData);
                    }
                }
             }
        };
        jTable.addMouseListener(_maDataRow);
 
        
    }
     public void SetSelectData()
     {
         jTable.setEnabled(false);
     }
}
